﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL.crm;
using Gift.Model.crm;
using Gift.Model.crm.eNum;

namespace WebSite.crm.company
{
    public partial class companyMoneyList : BaseCrm
    {
        readonly t_DALCompany theCompany = new t_DALCompany();




        public void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                base.ValidateLoginRule(eRule.InOutMoney);

                this.BindGrid();
            }
        }

        private void BindGrid()
        {

            Grid1.RecordCount = this.theCompany.GetRecordCount(GetSqlWhere);
            string strOrderBy = string.Format("{0} {1} "
                                               , ViewState["sort"] == null ? null : ViewState["sort"].ToString()
                                               , ViewState["dir"] == null ? null : ViewState["dir"].ToString());
            if (strOrderBy.Length < 13)
            {
                strOrderBy = "CompanyCode desc ";
            }
            DataTable table = this.theCompany.GetCompanyMoneyListByPage(GetSqlWhere, strOrderBy, Grid1.PageIndex, Grid1.PageSize).Tables[0];

            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };
        }



        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        string GetSqlWhere
        {
            get
            {
                StringBuilder sbWhere = new StringBuilder();
                sbWhere.AppendFormat("  (parentId = {0} or id={0} )", CompanyParamId);


                if (this.txtKeywords.Text.Trim() != "")
                {
                    sbWhere.AppendFormat(" and (Name like '%{0}%' or CompanyCode = '{0}' or CompanyShortName like '%{0}%')", this.txtKeywords.Text.Trim());
                }

                return sbWhere.ToString();
            }
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = theCompany.GetCompanyMoneyListByPage(GetSqlWhere, string.Format("{0} {1}", e.SortField, e.SortDirection), Grid1.PageIndex, Grid1.PageSize).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            ViewState["sort"] = e.SortField;
            ViewState["dir"] = e.SortDirection;
        }


        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.BindGrid();
        }




        string CompanyParamId
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    return Request.QueryString["id"];
                }
                else
                {
                    if (string.IsNullOrEmpty(base.CompanyId))
                    {
                        return "0";
                    }
                    else
                    {
                        return base.CompanyId;
                    }
                }
            }
        }



        protected void Grid1_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void btnBrachDeleted_Click(object sender, EventArgs e)
        {
            //string sIds = "";
            //for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            //{
            //    CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
            //    if (box.Checked)
            //    {
            //        sIds = sIds + this.GridView1.DataKeys[i].Value + ",";
            //    }
            //}
            //if (sIds.Length > 0)
            //{
            //    sIds = sIds.Remove(sIds.Length - 1);

            //    theCompany.DeleteMany(sIds);
            //    base.Response.Write("<script>alert('批量删除成功!')</script>");
            //    this.BindGridView(string.Empty);
            //}

            //    }

        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            this.BindGrid();
        }
    }
}